// pages/my/post/post.js
const app=getApp()
const db=wx.cloud.database().collection('Course');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    files:[],
    coach:"",
    name:'',
    weeks:['周一','周二','周三','周四','周五','周六','周日'],
    weekIndex:0,
    price:'',
    sort:['健身','跑步','球类','瑜伽'],
    sortIndex:0,
    describeDetail:'',
    startTime:['1','2','3','4','6','7','8','9','11','12','13'],
    STIndex:0,
    length:['1','2','3','4'],
    lengthIndex:0,
    homeType:['新品','热门'],
    HTIndex:0
  },

 

  chooseImage: function(){
    
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        console.log("当前时间戳为：" + timestamp);
        // tempFilePath可以作为files标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        wx.cloud.uploadFile({
          cloudPath: 'product/'+timestamp+'.png',
          filePath: tempFilePaths[0], // 文件路径
          success: function(res) {
            // get resource ID
            console.log('图片上传成功')
            that.setData({
              files:that.data.files.concat(res.fileID)
            })
          },
          fail: function(res) {
            console.log('图片上传失败'),
            console.error();
            
          }
        })
      }
    })
  },
  
  
deleteImage: function(e){
  var that=this;
  var files=that.data.files;
  var index=e.currentTarget.dataset.index;
  wx.showModal({
    title:'提示',
    content:'确定要删除此图片吗？',
    success: function(res){
      if(res.confirm){
        console.log('点击确定了');
        files.splice(index,1);
      }else if(res.cancel){
        console.log('点击取消了');
        return false;
      }
      that.setData({
        files:files
      })
      wx.cloud.deleteFile({
        fileList:[e.currentTarget.dataset.src],
        success:res=>{
          wx.showToast({
            title: '删除成功',
          })
        },
        fail:res=>{
          wx.showToast({
            title: '删除失败',
            icon:"none"
          })
        }
      })
    }
  })
},


files:function(e){
  this.setData({
    item:e.detail.value
  })
},
inputName: function(e){
  this.setData({
    name:e.detail.value
  })
},
chooseWeek: function(e){
  this.setData({
    weekIndex:e.detail.value
  })
},
inputPrice: function(e){
  this.setData({
    price:e.detail.value
  })
},
inputCoach: function(e){
  this.setData({
    coach:e.detail.value
  })
},
inputPlace:function(e){
  this.setData({
    place:e.detail.value
  })
},
chooseStartTime: function(e){
  console.log(e.detail)
  this.setData({
    STIndex: e.detail.value
  })
},
chooseLength: function(e){
  this.setData({
    lengthIndex: e.detail.value
  })
},
chooseSort: function(e){
  this.setData({
    sortIndex: e.detail.value
  })
},
chooseHomeType: function(e){
  this.setData({
    HTIndex: e.detail.value
  })
},
inputDetailText: function(e){
  this.setData({
    describeDetail:e.detail.value
  })
  
},
  readyForSubmit: function(){
    
    
    
    if(this.data.name==""){
      wx.showToast({
        title: '未填写课程名',
        icon:'none',
        duration:2000
      })
      return;
    }else if((parseInt(this.data.startTime[this.data.STIndex])<=4)&&(parseInt(this.data.startTime[this.data.STIndex])+parseInt(this.data.length[this.data.lengthIndex])-1>4)){
      wx.showToast({
        title: '课程不得跨越午间休息时段',
        icon:'none',
        duration:2000
      })
      return;
    }
    else if((parseInt(this.data.startTime[this.data.STIndex])<=9)&&((parseInt(this.data.startTime[this.data.STIndex])+parseInt(this.data.length[this.data.lengthIndex])-1)>9)){
      wx.showToast({
        title: '课程不得跨越晚间休息时段',
        icon:'none',
        duration:2000
      })
      return;
    }
    else if (parseInt(this.data.startTime[this.data.STIndex])+parseInt(this.data.length[this.data.lengthIndex])-1>13){
      wx.showToast({
        title: '课程时段最晚为第11段',
        icon:'none',
        duration:2000
      })
      return;
    }
    else if(this.data.price==""){
      wx.showToast({
        title: '未填写价格',
        icon:'none',
        duration:2000
      })
      return;
    }
    else if(this.data.coach==""){
      wx.showToast({
        title: '未填写教练',
        icon:'none',
        duration:2000
      })
      return;
    }
    else if(this.data.describeDetail==""){
      wx.showToast({
        title: '未填写详情',
        icon:'none',
        duration:2000
      })
      return;
    }else if(!this.data.files.length){
      wx.showToast({
        icon:'none',
        title: '未添加图片',
      })
    }
    

    else{
      wx.showLoading({
        title:'上传中',
      })
      
     
      
      
      db.add({
        data:{
          name:this.data.name,
          brief:this.data.describeDetail,
          coach:this.data.coach,
          hometype:this.data.homeType[this.data.HTIndex],
          startTime:this.data.startTime[this.data.STIndex],
          length:this.data.length[this.data.lengthIndex],
          place:this.data.place,
          price:this.data.price,
          sort:this.data.sort[this.data.sortIndex],
          week:this.data.weeks[this.data.weekIndex],
          posterURL:this.data.files[0],
          images:this.data.files,
        },
        success:function(res){
          wx.hideLoading()
          wx.showToast({
            title: '提交成功',
          })
          wx.redirectTo({
            url: '../Class_manage/Class_manage',
          })
        },
        fail:function(res){
          wx.hideLoading()
          wx.showToast({
            title: '提交失败',
            icon:"none"
          })
        }
      })
     
    
      
    }
       
    
    
  },

  onPullDownRefresh: function () {
    wx.setNavigationBarTitle({
      title: '发布闲置',
    });
    wx.showNavigationBarLoading();
    setTimeout(function(){
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    },2000)
  }
})